package com.shqcjd.devicestatus;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


import com.github.mikephil.charting.charts.LineChart;
import com.horen.chart.linechart.ILineChartData;
import com.horen.chart.linechart.LineChartHelper;
import com.shqcjd.bean.ChartBean;
import com.shqcjd.bean.TestLineData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.chart_equipment)
    LineChart chartEquipment;

    List<String> names = new ArrayList<>();
    ArrayList<ILineChartData> entries = new ArrayList<>();
    // 多条柱状图数据集合
    List<List<ILineChartData>> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

    }

    private void initMulitleLineChart(ChartBean chartBean) {
        //线的名字集合
        names.clear();
        entries.clear();
        data.clear();
        if (chartBean.temperature != null) {
            names.add("温度");
        }
        if (chartBean.humidity != null) {
            names.add("湿度");
        }
        if (chartBean.vibrateX != null) {
            names.add("振动X轴");
        }
        if (chartBean.vibrateY != null) {
            names.add("振动Y轴");
        }
        if (chartBean.vibrateZ != null) {
            names.add("振动Z轴");
        }

        for (int i = 0; i < names.size(); i++) {
//            // 单个柱状图数据
            ArrayList<ILineChartData> entries = new ArrayList<>();
//            for (int i1 = 0; i1 < 30; i1++) {
//                    entries.add(new TestLineData((Math.random() * 100), (i1 + 1) + "日"));
//
//            }
//            data.add(entries);
            String name = names.get(i);
            switch (name) {
                case "温度":
                    ChartBean.TemperatureBean temperature = chartBean.temperature;
                    if (temperature.dataVal == null) {
                        return;
                    }
                    if (temperature.dataVal.size() > 0) {
                        for (ChartBean.TemperatureBean.DataValBean dataValBean : temperature.dataVal) {
                            entries.add(new TestLineData(dataValBean.data, dataValBean.time+""));
                        }
                    }
                    break;
                case "湿度":
                    ChartBean.HumidityBean humidity = chartBean.humidity;
                    if (humidity.dataVal == null) {
                        return;
                    }
                    if (humidity.dataVal.size() > 0) {
                        for (ChartBean.HumidityBean.DataValBean dataValBean : humidity.dataVal) {
                            entries.add(new TestLineData(dataValBean.data, dataValBean.time+""));
                        }
                    }
                    break;
                case "振动X轴":
                    ChartBean.VibrateXBean vibrateX = chartBean.vibrateX;
                    if (vibrateX.dataVal == null) {
                        return;
                    }
                    if (vibrateX.dataVal.size() > 0) {
                        for (ChartBean.VibrateXBean.DataValBean dataValBean : vibrateX.dataVal) {
                            entries.add(new TestLineData(dataValBean.data, dataValBean.time+""));
                        }
                    }
                    break;
                case "振动Y轴":
                    ChartBean.VibrateYBean vibrateY = chartBean.vibrateY;
                    if (vibrateY.dataVal == null) {
                        return;
                    }
                    if (vibrateY.dataVal.size() > 0) {
                        for (ChartBean.VibrateYBean.DataValBean dataValBean : vibrateY.dataVal) {
                            entries.add(new TestLineData(dataValBean.data, dataValBean.time+""));
                        }
                    }
                    break;
                case "振动Z轴":
                    ChartBean.VibrateZBean vibrateZ = chartBean.vibrateZ;
                    if (vibrateZ.dataVal == null) {
                        return;
                    }
                    if (vibrateZ.dataVal.size() > 0) {
                        for (ChartBean.VibrateZBean.DataValBean dataValBean : vibrateZ.dataVal) {
                            entries.add(new TestLineData(dataValBean.data, dataValBean.time+""));
                        }
                    }
                    break;
            }
            data.add(entries);

        }
        LineChartHelper lineChartHelper = new LineChartHelper(chartEquipment);
        //颜色填充
        String[] colors = getResources().getStringArray(com.horen.chart.R.array.chart_colors);
        List<Integer> chartColors = new ArrayList<>();
        for (String color : colors) {
            chartColors.add(Color.parseColor(color));
        }
        //创建多条折线的图表
        lineChartHelper.showMultipleLineChart(data, names, chartColors, 6);
    }
}
